<?php

/**
 * Created by ghostsf
 * Number: n006833
 * Date: 2016/4/20
 */

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Course;
use app\admin\model\Subject;
use think\Db;
//require 'phpspider/autoload.php';
//use phpspider\core\phpspider;

/**
 * ajax 异步加载页面
 * Class Ajax
 * @package app\admin\controller
 */
class AjaxController extends CommonController {
    /**
     * 仪表盘
     */
    public function dashboard() {
        echo 1;die;
        $two = Db::table('ew_two')->find();
        $tables = Db::table('ew_table')->select();
        $this->assign('two',$two);
        $this->assign('tables',$tables);
        return $this->fetch('dashboard');
    }

    /**
     * 学生管理
     * @return mixed
     * $allin_rmb,$allin_ytb,$hui,$yd_in,$yd_out,$suanli,$benjin,$bili
     */
    public function student_nanage() {
        $subs = Db::table('ew_subject')->select();
        $this->assign('subs',$subs);
        return $this->fetch('student/manage');
    }
    /**
     * 学生口语预约
     * @return mixed
     * $allin_rmb,$allin_ytb,$hui,$yd_in,$yd_out,$suanli,$benjin,$bili
     */
    public function student_kouyu() {
        $subs = Db::table('ew_subject')->select();
        $this->assign('subs',$subs);
        return $this->fetch('student/kouyu');
    }
    /**
     * 作业预约
     * @return mixed
     * $allin_rmb,$allin_ytb,$hui,$yd_in,$yd_out,$suanli,$benjin,$bili
     */
    public function student_zuoye() {
        $subs = Db::table('ew_subject')->select();
        $this->assign('subs',$subs);
        return $this->fetch('student/homework');
    }


    /**
     * 老师管理
     */
    public function teacher_manage() {
        $student = Db::table('ew_student')->select();
        $this->assign('student',$student);
        return $this->fetch('teacher/manage');
    }

    /**
     * 课程管理
     */
    public function subject() {
        return $this->fetch('subject/manage');
    }








    /**
     * 钱包管理
     * @return mixed
     */
    public function pay_manage() {
        $id = session('loginSession')['user_id'];
        $admin = Db::table('ew_user')->where('user_id',$id)->find();
        $ready_rmb = 0;
        $ready_ytb = 0;
        $rmb = Db::table('ew_water')
            ->where(['water_userid'=>$id,'water_status'=>2,'water_type'=>5,'water_morb'=>1,'water_out_status'=>2])
            ->field('water_money')
            ->select();
        if(!empty($rmb)){
            foreach ($rmb as $v){
                $ready_rmb += $v['water_money'];
            }
        }
        $ytb = Db::table('ew_water')
            ->where(['water_userid'=>$id,'water_status'=>2,'water_type'=>5,'water_morb'=>2,'water_out_status'=>2])
            ->field('water_money')
            ->select();
        if(!empty($ytb)){
            foreach ($ytb as $v){
                $ready_ytb += $v['water_money'];
            }
        }
        $all_rmb = $admin['user_rmb']+$ready_rmb;
        $all_ytb = $admin['user_ytb']+$ready_ytb;
        $this->assign('admin',$admin);
        $this->assign('ready_rmb',$ready_rmb);
        $this->assign('ready_ytb',$ready_ytb);
        $this->assign('all_rmb',$all_rmb);
        $this->assign('all_ytb',$all_ytb);
        return $this->fetch('pay/manage');
    }
    /**
     * 设置
     */
    public function system_settings() {
        $id = session('loginSession')['user_id'];
        $admin = Db::table('ew_user')->where('user_id',$id)->find();
        $phone = substr_replace($admin['user_phone'],'****','3','4');
        $this->assign('admin',$admin);
        $this->assign('phone',$phone);
        return $this->fetch('system/manage');
    }
    /**
     * 流水管理
     */
    public function water_manage() {
        $id = session('loginSession')['user_id'];
        $admin = Db::table('ew_user')->where('user_id',$id)->find();
        $phone = substr_replace($admin['user_phone'],'****','3','4');
        $this->assign('admin',$admin);
        $this->assign('phone',$phone);
        return $this->fetch('water/manage');
    }

    //=================================我是用户和管理员身份的分割线==============================================

    /**
     * 参数设置
     * @return mixed
     */
    public function canshu() {
        $eth = Db::table('ew_eth')->find();
        $two = Db::table('ew_two')->find();
        $three = Db::table('ew_three')->find();
        $four = Db::table('ew_four')->find();
        $duan = Db::table('ew_duanxin')->where('id',13)->find();
        $today = date('Y-m-d');
        $this->assign('eth',$eth);
        $this->assign('two',$two);
        $this->assign('three',$three);
        $this->assign('four',$four);
        $this->assign('duan',$duan);
        $this->assign('today',$today);
        return $this->fetch('system/canshu');
    }

    /**
     * 算力管理
     * @return mixed
     */
    public function admin_suanli() {
        $all = Db::table('ew_computer')->count()*10;
        $use = Db::table('ew_computer')->sum('com_use');
        $have = Db::table('ew_computer')->sum('com_have');
        $allcount = Db::table('ew_computer')->sum('com_count');
        $usecount = ($use/$all)*$allcount;
        $havecount = ($have/$all)*$allcount;
        $usecount = substr(sprintf("%.3f", $usecount),0,-1);
        $havecount = substr(sprintf("%.3f", $havecount),0,-1);
        $this->assign('all',$all);
        $this->assign('use',$use);
        $this->assign('have',$have);
        $this->assign('allcount',$allcount);
        $this->assign('usecount',$usecount);
        $this->assign('havecount',$havecount);
        return $this->fetch('suanli/admin_manage');
    }
    /**
     * 合约管理
     */
    public function admin_contract() {
        $coms = Db::table('ew_computer')->field('com_id,com_number')->select();
        $this->assign('coms',$coms);
        return $this->fetch('contract/manage');
    }
    /**
     * 用户管理
     * @return mixed
     */
    public function user_manage() {
        $benjin = Db::table('ew_contract')->sum('con_money');
        $allin_rmb = Db::table('ew_water')
            ->where(['water_status'=>1,'water_type'=>1,'water_morb'=>1])
            ->sum('water_money');//人民币累计收益============
        //已回款
        $hui = 0;
        $hui_ytb = Db::table('ew_water')
            ->where(['water_status'=>1,'water_type'=>1,'water_morb'=>2])
            ->field('water_money,water_base')->select();
        if(!empty($hui_ytb)){
            foreach ($hui_ytb as $h){
                $hui += ($h['water_money']*$h['water_base']);
            }
        }
        $hui = $hui+$allin_rmb;
        $hui = substr(sprintf("%.3f", $hui),0,-1);
        $rmb = Db::table('ew_user')->where(['user_auth'=>1])->sum('user_rmb');
        $ytb = Db::table('ew_user')->where(['user_auth'=>1])->sum('user_ytb');

        $this->assign("benjin", $benjin);
        $this->assign("hui", $hui);
        $this->assign("rmb", $rmb);
        $this->assign("ytb", $ytb);
        return $this->fetch('admin/manage');
    }
    /**
     * 提现管理
     * @return mixed
     */
    public function admin_pay() {
        $rmb = Db::table('ew_water')
            ->where(['water_status'=>2,'water_type'=>5,'water_morb'=>1,'water_out_status'=>2])
            ->sum('water_money');
        $ytb = Db::table('ew_water')
            ->where(['water_status'=>2,'water_type'=>5,'water_morb'=>2,'water_out_status'=>2])
            ->where('water_money','>',0)
            ->sum('water_money');
        $many = Db::table('ew_water')
            ->where(['water_status'=>2,'water_type'=>5,'water_out_status'=>2])
            ->count();
        $this->assign('rmb', $rmb);
        $this->assign('ytb', $ytb);
        $this->assign('many', $many);
        return $this->fetch('pay/admin_manage');
    }
    /**
     * 财务流水
     * @return mixed
     */
    public function admin_water() {
        return $this->fetch('water/admin_manage');
    }
    /**
     * 短信设置
     */
    public function duanxin_set() {
        return $this->fetch('duanxin/manage');
    }
    /**
     * 算力产品配置
     */
    public function home_set() {
        $home = Db::table('ew_home')->select();
        $this->assign('home', $home);
        return $this->fetch('duanxin/home_two');
    }

    /**
     * 损耗统计
     */
    public function sunhao() {
        $rmb = Db::table('ew_admin_loss')->sum('rmb');
        $ytb = Db::table('ew_admin_loss')->sum('ytb');
        $this->assign('rmb', $rmb);
        $this->assign('ytb', $ytb);
        return $this->fetch('admin/sunhao');
    }














    /**
     * 优秀管理
     */
    public function youxiu_manage() {
        $sucai = Db::table('ew_sucai')->where('su_special',1)->select();
        $this->assign("sucai", $sucai);
        return $this->fetch('sucai/youxiu_manage');
    }
    /**
     * 广告管理
     * @return mixed
     */
    public function advert_manage() {
        return $this->fetch('config/advert');
    }

    /**
     * 特长管理
     * @return mixed
     */
    public function goodat_manage() {
        return $this->fetch('config/goodat');
    }












    /**
     * 菜单管理
     */
    public function menu_manage() {
        return $this->fetch('menu/manage');
    }



    /**
     * 科目管理
     * @return mixed
     */
    public function subject_manage() {
        return $this->fetch('subject/manage');
    }

    /**
     * 年级管理
     * @return mixed
     */
    public function grade_manage() {
        return $this->fetch('grade/manage');
    }



    /**
     * 系统设置-这留着，有用的，个性化设置，只有管理员才显示
     * @return mixed
     */
    public function personal_settings() {
        $configs = Db::table('ew_config')->where(array("type" => 1))->select();
        $this->assign('configs', $configs);
        return $this->fetch('config/manage');
    }


    /**
     * 教务设置--排课管理
     * @return mixed
     */
    public function course_arrange() {
        $param = $this->getDataByCampusid();
        $classes = Classes::all($param);
        $classroom = Classroom::all($param);
        $teachers = Teacher::all($param);
        $grades = Grade::all($param);
        $courses = Course::all($param);
        $this->assign('classes', $classes);
        $this->assign('classroom', $classroom);
        $this->assign('teachers', $teachers);
        $this->assign('grades', $grades);
        $this->assign('courses', $courses);
        return $this->fetch('arrange/manage');
    }

    /**
     * 教务设置--课表和点名
     * @return mixed
     */
    public function schedule_rollcall() {
        $campusid = session("loginSession")['campusid'];
        $classes = Db::name('classes')->where(["campusid" => $campusid])->select();
        $classrooms = Db::name('classroom')->where(["campusid" => $campusid])->select();
        $this->assign("classes", $classes);
        $this->assign("classrooms", $classrooms);
        return $this->fetch('schedule/manage');
    }

    /**
     * 出勤率����
     */
    public function gate_card() {
        $campusid = session("loginSession")['campusid'];
        $classes = Db::name('classes')->where(["campusid" => $campusid])->select();
        $courses = Db::name('course')->where(["campusid" => $campusid])->select();
        $this->assign("classes", $classes);
        $this->assign("courses", $courses);
        return $this->fetch("card/manage");
    }

    /**
     * �课消明细�����
     */
    public function school_lession() {
        $campusid = session("loginSession")['campusid'];
        $course = Db::name('course')->where(["campusid" => $campusid])->select();

        $this->assign("courses", $course);

        return $this->fetch("schoollession/manage");
    }



    
}
